#include "stdio.h"
#include "fcntl.h"  // open
#include "unistd.h" // read write
#include "errno.h"
#include "string.h"

int main(int argc, char const *argv[])
{

    int fd1 = open("test/sys_call_test.c", O_RDONLY);
    if (fd1 == -1)
    {
        // printf("open file1 failed  %d\n", errno);
        // printf("open file1 failed  %s\n", strerror(errno));
        perror("open file1 faile");
        return -1;
    }

    int fd2 = open("test/sys_call_test.c.bak", O_WRONLY | O_CREAT, 0777);

    if (fd2 == -1)
    {

        // printf("open file1 failed  %d\n", errno);
        printf("open file1 failed  %s\n", strerror(errno));
        return -1;
    }

    char buff[128];

    ssize_t read_len = 0;

    while ((read_len = read(fd1, buff, sizeof(buff))) > 0)
    {
        printf("%s", buff);
        write(fd2, buff, read_len);
    }

    close(fd1);
    close(fd2);

    return 0;
}
